﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using PPAP.Model;
using System.Data.OracleClient;

namespace PPAP.DAO
{
    public class PlantDAO
    {
        public PlantDAO() { }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Plant model)
        {
            model.ID = DbHelperOra.GetMaxID("ID", "PLANT");
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into PLANT(");
            strSql.Append("SUPPLIERID,SUPPILERNO,PLANTNAME,PLANTCODE,TELEPHONE,MAINBUSINESS,ADDRESS,ID,SUPPLIERADMIN)");
            strSql.Append(" values (");
            strSql.Append(":SUPPLIERID,:SUPPILERNO,:PLANTNAME,:PLANTCODE,:TELEPHONE,:MAINBUSINESS,:ADDRESS,:ID,:SUPPLIERADMIN)");
            OracleParameter[] parameters = {
					new OracleParameter(":SUPPLIERID", OracleType.Number,4),
					new OracleParameter(":SUPPILERNO", OracleType.VarChar,200),
					new OracleParameter(":PLANTNAME", OracleType.VarChar,200),
					new OracleParameter(":PLANTCODE", OracleType.VarChar,200),
					new OracleParameter(":TELEPHONE", OracleType.VarChar,200),
					new OracleParameter(":MAINBUSINESS", OracleType.VarChar,200),
					new OracleParameter(":ADDRESS", OracleType.VarChar,200),
					new OracleParameter(":ID", OracleType.Number,4),
                                          new OracleParameter(":SUPPLIERADMIN",OracleType.VarChar,200)};
            parameters[0].Value = model.SUPPLIERID;
            parameters[1].Value = model.SUPPILERNO;
            parameters[2].Value = model.PLANTNAME;
            parameters[3].Value = model.PLANTCODE;
            parameters[4].Value = model.TELEPHONE;
            parameters[5].Value = model.MAINBUSINESS;
            parameters[6].Value = model.ADDRESS;
            parameters[7].Value = model.ID;
            parameters[8].Value = model.Supplieradmin;

          return  DbHelperOra.ExecuteSql(strSql.ToString(), parameters)>0?int.Parse(model.ID.ToString()):0;
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public int Update(Plant model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update PLANT set ");
            strSql.Append("SUPPLIERID=:SUPPLIERID,");
            strSql.Append("SUPPILERNO=:SUPPILERNO,");
            strSql.Append("PLANTNAME=:PLANTNAME,");
            strSql.Append("PLANTCODE=:PLANTCODE,");
            strSql.Append("TELEPHONE=:TELEPHONE,");
            strSql.Append("MAINBUSINESS=:MAINBUSINESS,");
            strSql.Append("ADDRESS=:ADDRESS,");
            strSql.Append("ID=:ID,SUPPLIERADMIN=:SUPPLIERADMIN");
            strSql.Append(" where id="+model.ID);
            OracleParameter[] parameters = {
					new OracleParameter(":SUPPLIERID", OracleType.Number,4),
					new OracleParameter(":SUPPILERNO", OracleType.VarChar,200),
					new OracleParameter(":PLANTNAME", OracleType.VarChar,200),
					new OracleParameter(":PLANTCODE", OracleType.VarChar,200),
					new OracleParameter(":TELEPHONE", OracleType.VarChar,200),
					new OracleParameter(":MAINBUSINESS", OracleType.VarChar,200),
					new OracleParameter(":ADDRESS", OracleType.VarChar,200),
					new OracleParameter(":ID", OracleType.Number,4),
                                           new OracleParameter(":SUPPLIERADMIN",OracleType.VarChar,200)};
            parameters[0].Value = model.SUPPLIERID;
            parameters[1].Value = model.SUPPILERNO;
            parameters[2].Value = model.PLANTNAME;
            parameters[3].Value = model.PLANTCODE;
            parameters[4].Value = model.TELEPHONE;
            parameters[5].Value = model.MAINBUSINESS;
            parameters[6].Value = model.ADDRESS;
            parameters[7].Value = model.ID;
            parameters[8].Value = model.Supplieradmin;

           return DbHelperOra.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public int Delete(int id)
        {
            //该表无主键信息，请自定义主键/条件字段

            string sql = "delete contactperson where plantid="+id;
            DbHelperOra.ExecuteSql(sql);  //情况该工厂下所有联系人
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from PLANT ");
            strSql.Append(" where id="+id);
            OracleParameter[] parameters = {
};

           return DbHelperOra.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Plant GetModel(int id)
        {
            //该表无主键信息，请自定义主键/条件字段
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select SUPPLIERID,SUPPILERNO,PLANTNAME,PLANTCODE,TELEPHONE,MAINBUSINESS,ADDRESS,ID,SUPPLIERADMIN from PLANT ");
            strSql.Append(" where id="+id);
            OracleParameter[] parameters = {
};

            Plant model = new Plant();
            DataSet ds = DbHelperOra.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["SUPPLIERID"].ToString() != "")
                {
                    model.SUPPLIERID = int.Parse(ds.Tables[0].Rows[0]["SUPPLIERID"].ToString());
                }
                model.SUPPILERNO = ds.Tables[0].Rows[0]["SUPPILERNO"].ToString();
                model.PLANTNAME = ds.Tables[0].Rows[0]["PLANTNAME"].ToString();
                model.PLANTCODE = ds.Tables[0].Rows[0]["PLANTCODE"].ToString();
                model.TELEPHONE = ds.Tables[0].Rows[0]["TELEPHONE"].ToString();
                model.MAINBUSINESS = ds.Tables[0].Rows[0]["MAINBUSINESS"].ToString();
                model.ADDRESS = ds.Tables[0].Rows[0]["ADDRESS"].ToString();
                if (ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                model.Supplieradmin = ds.Tables[0].Rows[0]["SUPPLIERADMIN"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select SUPPLIERID,SUPPILERNO,PLANTNAME,PLANTCODE,TELEPHONE,MAINBUSINESS,p.ADDRESS,p.ID,rownum,s.suppliername,s.suppliercode ");
            strSql.Append(" from plant p left join supplier s on s.id=p.supplierid ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperOra.Query(strSql.ToString());
        }

       
    }
}
